<?php
/**
 * 酱茄Free主题由酱茄（www.jiangqie.com）开发的一款免费开源的WordPress主题，专为WordPress博客、资讯、自媒体网站而设计。
 */

add_action('widgets_init', 'jiangqie_tags');

function jiangqie_tags()
{
	register_widget('JQ_Widget_Tags');
}

class JQ_Widget_Tags extends WP_Widget
{
	function __construct()
	{
		$widget_ops = ['classname' => 'jaingqie-widget-tags', 'description' => '显示热门标签'];
		$control_ops = [];
        parent::__construct('jaingqie-widget-tags', '酱茄-标签云', $widget_ops, $control_ops);
	}

	function widget($args, $instance)
	{
		extract($args);

		$title = apply_filters('widget_name', $instance['title']);
		$count = $instance['count'];
		$offset = $instance['offset'];
		$more = $instance['more'];
		$link = $instance['link'];

		$mo = '';
		if ($more != '' && $link != '') {
			$mo = '<a class="btn-more" href="' . $link . '">' . $more . '</a>';
		}

		echo $before_widget;

		echo $before_title. $title . $mo . $after_title;
		echo '<div class="tag-list">';
		$tags_list = get_tags('orderby=count&order=DESC&number=' . $count . '&offset=' . $offset);
		if ($tags_list) {
			foreach ($tags_list as $tag) {
				echo '<a title="' . $tag->count . '个话题" href="' . get_tag_link($tag) . '">' . $tag->name . ' (' . $tag->count . ')</a>';
			}
		} else {
			echo '暂无标签！';
		}
		echo '</div>';

		echo $after_widget;
	}

	function form($instance)
	{
?>
		<p>
			<label>
				名称：
				<input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance['title']; ?>" class="widefat" />
			</label>
		</p>
		<p>
			<label>
				显示数量：
				<input id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="number" value="<?php echo $instance['count']; ?>" class="widefat" />
			</label>
		</p>
		<p>
			<label>
				去除前几个：
				<input id="<?php echo $this->get_field_id('offset'); ?>" name="<?php echo $this->get_field_name('offset'); ?>" type="number" value="<?php echo $instance['offset']; ?>" class="widefat" />
			</label>
		</p>
		<p>
			<label>
				More 显示文字：
				<input style="width:100%;" id="<?php echo $this->get_field_id('more'); ?>" name="<?php echo $this->get_field_name('more'); ?>" type="text" value="<?php echo $instance['more']; ?>" size="24" />
			</label>
		</p>
		<p>
			<label>
				More 链接：
				<input style="width:100%;" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" type="url" value="<?php echo $instance['link']; ?>" size="24" />
			</label>
		</p>
<?php
	}
}

?>